package com.diyizu.service.impl;

import com.diyizu.dao.IUserDao;
import com.diyizu.dao.impl.IUserDaoImpl;
import com.diyizu.dao.impl.ReadLogDao;
import com.diyizu.domain.User;
import com.diyizu.factory.Factory;
import com.diyizu.service.IRegService;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Scanner;

public class IRegServiceImpl implements IRegService {
    private static Scanner sc = new Scanner(System.in);
    private static IUserDao iUserDao = Factory.getIUserDaoImpl();
    ReadLogDao readLogDao = Factory.getReadLogDao();

    @Override
    public void userReg(String username, String password, String email) {
        //增
        iUserDao.addUser(username,password,email);
    }

    @Override
    //改密码
    public void updatePass(String username, String password) {
        iUserDao.updatePass(username,password);
    }

    @Override
    //改邮箱
    public void updateEmail(String username, String email) {
        iUserDao.updateEmail(username,email);
    }

    @Override
    //判断用户名格式是否正确
    public boolean isUserFormat(String username) {
        char[] chars = username.toCharArray();
        //首位必须是字母大小写都可以
        if (('a' <= chars[0] && chars[0] <= 'Z') || ('A' <= chars[0] && chars[0] <= 'Z')) {
            if (10 <= username.length() && username.length() <= 20) {
                for (char c : chars) {
                    if (c != ' ') {
                        return true;
                    } else {
                        System.out.println("提示：不能出现空格");
                    }
                }
            } else {
                System.out.println("提示：用户名长度为10到20个字符或数字");
            }
        } else {
            System.out.println("提示：首位必须字母开头");
        }
        return false;
    }

    @Override
    //判断邮箱是否已被绑定
    public boolean isEmail(String email) {
        ArrayList<User> allUser = iUserDao.getUser();
        for (User s : allUser) {
            if (s.getEmail().equals(email)){
                return true;
            }
        }
        return false;
    }

    @Override
    //获取用户
    public User getUser(String username) {
        ArrayList<User> users = iUserDao.getUser();
        for (User user : users) {
            if (user.getUsername().equals(username)){
                return user;
            }
        }
        return null;
    }

    @Override
    //判断邮箱验证码是否正确
    public boolean isIdentifyingCode(String code) {
        //判断验证码是否正确
        System.out.println("请输入验证码");
        String str = sc.next();
        if (code.equals(str)){
            return true;
        }
        return false;
    }

    @Override
    //判断密码格式是否正确
    public boolean isPassFormat(String password) {
        char[] chars = password.toCharArray();
        if (password.length() >= 8 && password.length() <= 16) {
            for (char c : chars) {
                if (c != ' ') {
                    return true;
                } else {
                    System.out.println("提示：不能出现空格");
                }
            }
        }else{
            System.out.println("提示：密码长度为8到16个字符或数字");
        }
        return false;
    }

    @Override
    public String getUserRegTime(String userName) {
        String userRegTime = null;
        ArrayList<String> log = readLogDao.getLog();
        for (int i = log.size() - 1; i >= 0; i--) {
            if (log.get(i).endsWith(userName+"注册成功")){
                userRegTime = log.get(i).split(",")[0];
            }
        }
        return userRegTime;
    }

}
